﻿@section header{

}
<partial name="_page_list" />
@section scripts{
 <script>
    var baseUrl = "/rbac/Role/";
    $(function () {
        gridList();
    })
    function gridList() {
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            url: baseUrl + "GetGridJson",
            height: $(window).height() - 228,
            mtype: "POST",
            colModel: [
                { label: "主键", name: "ID", hidden: true, key: true },
                { label: '角色名称', name: 'Name', width: 200, align: 'left' },
                {
                    label: '角色类型', name: 'Type', width: 80, align: 'left',
                    formatter: function (cellvalue) {
                        try {
                            return top.clients.dataItems["JSLX"][cellvalue] == undefined ? "" :
                                top.clients.dataItems["JSLX"][cellvalue]
                        } catch (err) {
                            return "";
                        }
                    }
                },
                {
                    label: "有效", name: "Status", align: "center",
                    formatter: function (cellvalue, options, rowObject) {
                        var keyId = rowObject.ID;
                        return cellvalue == 0 ? "<i onclick='Switch(\"" + keyId + "\")' class=\"fa fa-toggle-on\"></i>" : "<i onclick='Switch(\"" + keyId + "\")' class=\"fa fa-toggle-off\"></i>";
                    }
                },
                {
                    label: '排序', name: 'OrderBy', align: 'left',
                    formatter: function (cellvalue, options, rowObject) {
                        var keyId = rowObject.ID;
                        return "<input type='text' name='OrderBy[" + keyId + "]' autocomplete='off' style='width: 50px; height: 20px;' onchange='update_field(\"" + keyId + "\",this.value)' value=" + cellvalue + " />";
                    }
                },
                { label: '添加时间', name: 'AddTime', width: 300, align: 'left' }
            ],
            rowNum: 20,
            rowList: [20, 50, 100],
            pager: '#gridPager',
            sortname: 'OrderBy',
            sortorder: "asc",
            viewrecords: true,
            autowidth: true,
            multiselect: true
        });
        $("#btn_search").click(function () {
            $gridList.jqGrid('setGridParam', {
                postData: { keyword: $("#txt_keyword").val() },
            }).trigger('reloadGrid');
        });
    }
    function btn_add() {
        $.modalOpen({
            id: "Form",
            title: "新增",
            url: baseUrl + "Form",
            width: "550px",
            height: "570px",
            btn: null
        });
    }
    function btn_edit(id) {
        if (id == "" || id == undefined) {
            var ids = gridselId();
            console.log(ids);
            if (ids == "" || ids == undefined) {
                $.modalAlert("请选择一条数据", "warning");
                return;
            }
            if (ids.length > 1) {
                $.modalAlert("不能选择多条数据", "warning");
                return;
            }
            id = ids[0];
        }
        $.modalOpen({
            id: "Form",
            title: "修改",
            url: baseUrl + "Form?keyValue=" + id,
            width: "600px",
            height: "800px",
            btn: null
        });
    }
     function btn_delete() {
         if (id == "" || id == undefined) {
             var ids = gridselId();
             if (ids == "" || ids == undefined) {
                 $.modalAlert("请选择一条数据", "warning");
                 return;
             }
             id = ids;
         }
         $.deleteForm({
             url: baseUrl + "DeleteForm",
             param: { keyValue: id.join(",") },
             success: function () {
                 $("#gridList").resetSelection();
                 $("#gridList").trigger("reloadGrid");
             }
         })
    }
    function btn_details() {
        var keyValue = $("#gridList").jqGridRowValue().ID;
        $.modalOpen({
            id: "Details",
            title: "查看",
            url: baseUrl + "Details?keyValue=" + keyValue,
            width: "700px",
            height: "490px",
            btn: null,
        });
     }
     // 1,2,3,4
     function gridselId() {
         var myArray = new Array()
         var s = $("#gridList").jqGrid('getGridParam', 'selarrrow');
         if (s.length > 0) {
             for (var i = 0; i < s.length; i++) {
                 if (s[i]) {
                     var ret = $("#gridList").jqGrid('getRowData', s[i]);
                     myArray.push(ret.ID);
                 }
             }
         } else {
             myArray.push($("#gridList").jqGridRowValue().ID);
         }
         return myArray;
     }
     //关闭有效触发
     function Switch(keyId) {
         $.ajax({
             url: baseUrl + "Switch?keyId=" + keyId,
             data: {},
             type: "POST",
             dataType: "json",
             async: false,
             success: function () {
                 $.currentWindow().$("#gridList").resetSelection();
                 $.currentWindow().$("#gridList").trigger("reloadGrid");
             }
         });
     }
     function update_field(keyId, cellvalue) {
         $.ajax({
             url: baseUrl + "UpdateOrderBy?keyId=" + keyId + "&value=" + cellvalue,
             data: {},
             dataType: "json",
             type: "POST",
             success: function () {
                 $.currentWindow().$("#gridList").resetSelection();
                 $.currentWindow().$("#gridList").trigger("reloadGrid");
             }
         });
     }
</script>
}